package com.ciwong.epaper.modules.evaluate.ui;

import android.media.MediaPlayer;
import android.media.PlaybackParams;
import android.os.Build;

import com.ciwong.epaper.application.EApplication;
import com.ciwong.epaper.modules.evaluate.bean.Sentence;
import com.ciwong.epaper.util.ESystem;
import com.ciwong.epaper.widget.evaluate.SentenceSpeechView;
import com.ciwong.libs.audio.play.AudioPlayer;

/**
 * Created by lzr on 2017/4/17.
 * 独立通读
 */
public class SpeechIndependentController extends SpeechPlayController {


    public SpeechIndependentController() {

    }

    public SpeechIndependentController(SentenceSpeechView speechView, int typeMode, String uuid, long startTime, boolean serviceExpired) {
        super(speechView, typeMode, uuid, startTime, serviceExpired);
    }


    @Override
    protected void playWord(int index, boolean autoPlay) {
        if (isPause) {
            return;
        }
        Sentence sentence = sentences.get(index);
        String url = ESystem.formatPath(sentence.getSentenceMp3());// .replace("
        if (url == null) {
            url = "";
        }
        AudioPlayer.getInstance().setOnPlayListener(listener).play(url,
                false);
        try {
            if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M && EApplication.getInstance().mPlayerspeed != 1.0f) {
                MediaPlayer mMediaPlayer = AudioPlayer.getInstance().getMediaPlayer();
                if (mMediaPlayer != null) {
                    try {
                        PlaybackParams params = mMediaPlayer.getPlaybackParams().setSpeed(EApplication.getInstance().mPlayerspeed);
                        if (params != null) {
                            mMediaPlayer.setPlaybackParams(params);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void getPlayTime(long duration, Object mp3) {
        if (ASIDE_AUDIO_TAG.equals(mp3)) {
            return;
        }
        // 获取播放单词时长
        if (!"pcm".equals(mp3 + "")) {
            audioDuration = duration + RECORD_ADD_DURATION;
            startRecordHint(currentListenAndRepeatIndex);
        }
    }
}
